import { RosterInterface } from '@/api/interface/personnel/roster/roster';
import { BasicForm } from '@/components/ProForm/interface';
import { PublicInterface } from '@/interface/public';
import dayjs from 'dayjs';

/**
 * 合同信息
 */

export function useContractInformationForm(proTableBox: PublicInterface.ProTableValue) {
  let contractInformationForm = reactive<BasicForm<RosterInterface.ReqContractInfoSave>>({
    form: {
      id: null,
      start: '',
      end: '',
      ageLimit: null,
      dateOfSigning: '',
      remark: '',
      rosterId: null
    },
    title: '',
    minWidth: 660,
    labelWidth: [100, 120],
    visible: false,
    formAttrs: {
      labelWidth: '120px'
    },
    options: [
      {
        type: 'input',
        label: 'ID',
        prop: 'id',
        hide: true
      },
      {
        type: 'input',
        label: '主表id',
        prop: 'rosterId',
        hide: true
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'start',
        label: '开始日期',
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        },
        rules: [
          {
            required: true,
            message: '开始日期',
            trigger: 'change'
          }
        ],
        changeMethod: () => setAgeYearCount()
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'end',
        label: '结束日期',
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        },
        rules: [
          {
            required: true,
            message: '结束日期',
            trigger: 'change'
          }
        ],
        changeMethod: () => setAgeYearCount()
      },
      {
        type: 'input',
        hide: false,
        prop: 'ageLimit',
        label: '年限',
        attrs: {},
        rules: [
          {
            required: true,
            message: '年限',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'dateOfSigning',
        label: '签订日期',
        defaultValue: dayjs().format(BasicKey.DATE_FORMAT.YYYY_MM_DD),
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        },
        rules: [
          {
            required: true,
            message: '签订日期',
            trigger: 'change'
          }
        ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'remark',
        label: 'remark',
        attrs: {
          style: {
            width: '500px'
          }
        }
      }
    ]
  });

  /**
   * 计算年限
   */
  const setAgeYearCount = () => {
    const { start, end } = contractInformationForm.form;
    if (start && end) {
      contractInformationForm.form.ageLimit = dayjs(end).diff(start, 'year'); // 10（年）
    }
  };

  /**
   * @description: 保存表单数据
   * @return {*}
   */
  const saveAddContractInformationForm = async form => {
    await contractInfoSave(form);
    contractInformationForm.visible = false;
    elMessage();
    proTableBox.value?.getTableList();
  };

  return { contractInformationForm, saveAddContractInformationForm };
}
